%set(gcf, 'Renderer', 'painters');
%set(gca,'nextplot','replacechildren');
%set(gca,'replace');
%set(gcf,'Renderer','zbuffer');
close all;
clear all;

bemfr = VideoWriter('bemt_fr1_2.avi','Uncompressed AVI');
%reccfr = VideoWriter('reccfr3.avi','Uncompressed AVI');
%cdc42fr = VideoWriter('cdc42fr3.avi','Uncompressed AVI');
bemfr.FrameRate = 1;
%reccfr.FrameRate = 3;
%cdc42fr.FrameRate = 3;
open(bemfr);
%open(reccfr);
%open(cdc42fr);

fcount=10;
for i=1:fcount;
    fname=fullfile('.',num2str(i),'/data_bemgeft_time_course');
    vname=fullfile('.',num2str(i),'/video_bemgeft_time_course');
    bemfr = VideoWriter('bemt_fr1_2.avi','Uncompressed AVI');
bemt=importdata('data_bemt_time_course',' ');

N = 100;                            % number of spatial grid points
Cellsize = 5;                       % sphere diameter (microns)
dx  = Cellsize*sqrt(pi)/N; 

N=size(bemt,2);
fig2=figure(2);
hold off;
if mod(size(bemt,1),size(bemt,2))~=0
    display('Some frames were not fully written! The further progression of the script will be erroneous\n');
else
    Nf=size(bemt,1)/size(bemt,2);
    fig2=figure(2);
    %clf reset;
    mcoord=zeros(Nf,2);
    for i=0:(Nf-1)
        bem=bemt(1+i*N:N+i*N,:);
        [xind,yind]=find(bem>mean(mean(bem)));
        mcoord(i+1,:)=[mean(xind)*dx, mean(yind)*dx];
        image(bem);
        title('Receptor bound to pheromone');
        fr=getframe(fig2);
        writeVideo(bemfr,fr);
    end
end
figure(3);
hold on;
scatter(mcoord(:,1),mcoord(:,2));
plot(mcoord(:,1),mcoord(:,2));
xlim([0 N*dx]);
ylim([0 N*dx]);
title('Movement of the geometric center of BemGEF patch');
%set(gcf, 'Renderer', 'painters');
close(bemfr);
%reccfr.close;
%cdc42fr.close;